//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "Fd2dBlackScholesVanillaEngine.h"
using namespace Cephei::QL::Pricingengines::Basket;
#include <gen/QL/Processes/GeneralizedBlackScholesProcess.h>
#include <gen/QL/Methods/Finitedifferences/Solvers/FdmSchemeDesc.h>
using namespace Cephei::QL::Processes;
using namespace Cephei::QL::Methods::Finitedifferences::Solvers;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Pricingengines::Basket::CFd2dBlackScholesVanillaEngine::CFd2dBlackScholesVanillaEngine (Cephei::QL::Processes::IGeneralizedBlackScholesProcess^ p1, Cephei::QL::Processes::IGeneralizedBlackScholesProcess^ p2, Double correlation, Microsoft::FSharp::Core::FSharpOption<UInt64>^ xGrid, Microsoft::FSharp::Core::FSharpOption<UInt64>^ yGrid, Microsoft::FSharp::Core::FSharpOption<UInt64>^ tGrid, Microsoft::FSharp::Core::FSharpOption<UInt64>^ dampingSteps, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Methods::Finitedifferences::Solvers::IFdmSchemeDesc^>^ schemeDesc) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CGeneralizedBlackScholesProcess^ _Cp1;
    CGeneralizedBlackScholesProcess^ _Cp2;
    CFdmSchemeDesc^ _CschemeDesc;
    try
    {
#ifdef HANDLE
        _phFd2dBlackScholesVanillaEngine = NULL;
#endif
        _Cp1 = safe_cast<CGeneralizedBlackScholesProcess^> (p1);
        _Cp1->Lock();
        boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess>& _p1 = static_cast<boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess>&> (_Cp1->GetShared ()); 
        _Cp2 = safe_cast<CGeneralizedBlackScholesProcess^> (p2);
        _Cp2->Lock();
        boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess>& _p2 = static_cast<boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess>&> (_Cp2->GetShared ()); 
        QuantLib::Real _correlation = (QuantLib::Real)ValueHelper::Convert (correlation); //d
        QuantLib::Size _xGrid = 
            (Microsoft::FSharp::Core::FSharpOption<UInt64>::IsSome::get (xGrid) ? (QuantLib::Size)ValueHelper::Convert (xGrid->Value) : 100); //4
        QuantLib::Size _yGrid = 
            (Microsoft::FSharp::Core::FSharpOption<UInt64>::IsSome::get (yGrid) ? (QuantLib::Size)ValueHelper::Convert (yGrid->Value) : 100); //4
        QuantLib::Size _tGrid = 
            (Microsoft::FSharp::Core::FSharpOption<UInt64>::IsSome::get (tGrid) ? (QuantLib::Size)ValueHelper::Convert (tGrid->Value) : 50); //4
        QuantLib::Size _dampingSteps = 
            (Microsoft::FSharp::Core::FSharpOption<UInt64>::IsSome::get (dampingSteps) ? (QuantLib::Size)ValueHelper::Convert (dampingSteps->Value) : 0); //4
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Methods::Finitedifferences::Solvers::IFdmSchemeDesc^>::IsSome::get (schemeDesc))
        {
            _CschemeDesc = safe_cast<CFdmSchemeDesc^> (schemeDesc->Value);
            _CschemeDesc->Lock();
        }
        QuantLib::FdmSchemeDesc& _schemeDesc = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Methods::Finitedifferences::Solvers::IFdmSchemeDesc^>::IsSome::get (schemeDesc) ? static_cast<QuantLib::FdmSchemeDesc&> (_CschemeDesc->GetReference ()) : QuantLib::FdmSchemeDesc::Hundsdorfer()); //1
        _ppFd2dBlackScholesVanillaEngine = new boost::shared_ptr<QuantLib::Fd2dBlackScholesVanillaEngine> (new QuantLib::Fd2dBlackScholesVanillaEngine ( _p1,  _p2,  _correlation,  _xGrid,  _yGrid,  _tGrid,  _dampingSteps,  _schemeDesc ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cp1 != nullptr) _Cp1->Unlock();
        if (_Cp2 != nullptr) _Cp2->Unlock();
        if (_CschemeDesc != nullptr) _CschemeDesc->Unlock();
    }
}
Cephei::QL::Pricingengines::Basket::CFd2dBlackScholesVanillaEngine::CFd2dBlackScholesVanillaEngine (boost::shared_ptr<QuantLib::Fd2dBlackScholesVanillaEngine>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFd2dBlackScholesVanillaEngine = NULL;
#endif
	_ppFd2dBlackScholesVanillaEngine = &childNative;
    
}
Cephei::QL::Pricingengines::Basket::CFd2dBlackScholesVanillaEngine::CFd2dBlackScholesVanillaEngine (QuantLib::Fd2dBlackScholesVanillaEngine& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phFd2dBlackScholesVanillaEngine = NULL;
#endif
	_ppFd2dBlackScholesVanillaEngine = new boost::shared_ptr<QuantLib::Fd2dBlackScholesVanillaEngine> (&childNative);
    
    _Fd2dBlackScholesVanillaEngineOwner = owner;
    
}

Cephei::QL::Pricingengines::Basket::CFd2dBlackScholesVanillaEngine::CFd2dBlackScholesVanillaEngine (CFd2dBlackScholesVanillaEngine^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFd2dBlackScholesVanillaEngine = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFd2dBlackScholesVanillaEngine = new boost::shared_ptr<QuantLib::Fd2dBlackScholesVanillaEngine> (copy->GetShared());
        
    }
}
Cephei::QL::Pricingengines::Basket::CFd2dBlackScholesVanillaEngine::CFd2dBlackScholesVanillaEngine (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFd2dBlackScholesVanillaEngine = NULL;
#endif
	if (!t->IsSubclassOf(CFd2dBlackScholesVanillaEngine::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Pricingengines::Basket::CFd2dBlackScholesVanillaEngine::CFd2dBlackScholesVanillaEngine (QuantLib::Handle<QuantLib::Fd2dBlackScholesVanillaEngine>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phFd2dBlackScholesVanillaEngine = &childNative;
	_ppFd2dBlackScholesVanillaEngine = &static_cast<boost::shared_ptr<QuantLib::Fd2dBlackScholesVanillaEngine>>(childNative.currentLink());
    
    _Fd2dBlackScholesVanillaEngineOwner = owner;
}
Cephei::QL::Pricingengines::Basket::CFd2dBlackScholesVanillaEngine::CFd2dBlackScholesVanillaEngine (QuantLib::Handle<QuantLib::Fd2dBlackScholesVanillaEngine> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phFd2dBlackScholesVanillaEngine = &childNative;
	_ppFd2dBlackScholesVanillaEngine = &static_cast<boost::shared_ptr<QuantLib::Fd2dBlackScholesVanillaEngine>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Pricingengines::Basket::CFd2dBlackScholesVanillaEngine::CFd2dBlackScholesVanillaEngine (QuantLib::Fd2dBlackScholesVanillaEngine childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFd2dBlackScholesVanillaEngine = NULL;
#endif
	_ppFd2dBlackScholesVanillaEngine = new boost::shared_ptr<QuantLib::Fd2dBlackScholesVanillaEngine> (new QuantLib::Fd2dBlackScholesVanillaEngine (childNative));
    
}
#endif

Cephei::QL::Pricingengines::Basket::CFd2dBlackScholesVanillaEngine::~CFd2dBlackScholesVanillaEngine ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppFd2dBlackScholesVanillaEngine != NULL)
    {
	    delete _ppFd2dBlackScholesVanillaEngine;
        _ppFd2dBlackScholesVanillaEngine = NULL;
    }
}
Cephei::QL::Pricingengines::Basket::CFd2dBlackScholesVanillaEngine::!CFd2dBlackScholesVanillaEngine ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppFd2dBlackScholesVanillaEngine != NULL)
    {
	    delete _ppFd2dBlackScholesVanillaEngine;
    }
}
QuantLib::Fd2dBlackScholesVanillaEngine& Cephei::QL::Pricingengines::Basket::CFd2dBlackScholesVanillaEngine::GetReference ()
{
    if (_ppFd2dBlackScholesVanillaEngine == NULL) throw REFNEW NativeNullException ();
	return **_ppFd2dBlackScholesVanillaEngine;
}
boost::shared_ptr<QuantLib::Fd2dBlackScholesVanillaEngine>& Cephei::QL::Pricingengines::Basket::CFd2dBlackScholesVanillaEngine::GetShared ()
{
    if (_ppFd2dBlackScholesVanillaEngine == NULL) throw REFNEW NativeNullException ();
	return *_ppFd2dBlackScholesVanillaEngine;
}
QuantLib::Fd2dBlackScholesVanillaEngine* Cephei::QL::Pricingengines::Basket::CFd2dBlackScholesVanillaEngine::GetPointer ()
{
    if (_ppFd2dBlackScholesVanillaEngine == NULL) throw REFNEW NativeNullException ();
	return &**_ppFd2dBlackScholesVanillaEngine;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::Fd2dBlackScholesVanillaEngine>& Cephei::QL::Pricingengines::Basket::CFd2dBlackScholesVanillaEngine::GetHandle ()
{
	if (_phFd2dBlackScholesVanillaEngine == NULL)
	{
		_phFd2dBlackScholesVanillaEngine = new Handle<QuantLib::Fd2dBlackScholesVanillaEngine> (*_ppFd2dBlackScholesVanillaEngine);
	}
	return *_phFd2dBlackScholesVanillaEngine;
}
#endif
bool Cephei::QL::Pricingengines::Basket::CFd2dBlackScholesVanillaEngine::HasNative () 
{
	return (_ppFd2dBlackScholesVanillaEngine != NULL);
}

Cephei::QL::Pricingengines::Basket::IFd2dBlackScholesVanillaEngine^ Cephei::QL::Pricingengines::Basket::CFd2dBlackScholesVanillaEngine::Calculate::get ()
{
    try
    {
    	(*_ppFd2dBlackScholesVanillaEngine)->calculate ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Pricingengines::Basket::IFd2dBlackScholesVanillaEngine^ Cephei::QL::Pricingengines::Basket::CFd2dBlackScholesVanillaEngine_Factory::Create (Cephei::QL::Processes::IGeneralizedBlackScholesProcess^ p1, Cephei::QL::Processes::IGeneralizedBlackScholesProcess^ p2, Double correlation, Microsoft::FSharp::Core::FSharpOption<UInt64>^ xGrid, Microsoft::FSharp::Core::FSharpOption<UInt64>^ yGrid, Microsoft::FSharp::Core::FSharpOption<UInt64>^ tGrid, Microsoft::FSharp::Core::FSharpOption<UInt64>^ dampingSteps, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Methods::Finitedifferences::Solvers::IFdmSchemeDesc^>^ schemeDesc)
{
    return REFNEW CFd2dBlackScholesVanillaEngine ( p1,  p2,  correlation,  xGrid,  yGrid,  tGrid,  dampingSteps,  schemeDesc);
}
